import { FormSchema } from "@/types/schema";
import { getAccountRoles, getAccountRoleInfo } from "@/api/system/ums"

// 与vue2的里面的data一样，函数返回新对象防止多处共用同一对象,造成数据混乱
export const getRoleSchema = (): FormSchema => ({
    formItem: [
        {
            type: "checkbox",
            label: "角色",
            field: "roleIds",
            value: [],
            options: [],
            loading: true,
            rules: [
                {
                    required: true,
                    message: '请选择角色',
                    type: 'array'
                }
            ],
            props: {
                placeholder: "请选择"
            },
            asyncValue: async (currentValue, formInstance) => {
                const { id } = formInstance?.props.fields as any
                // 获取角色信息
                const data = await getAccountRoleInfo(id)
                // 设置角色复选框默认值
                return data.map(item => item.id)
            },
            asyncOptions: async (currentValue, formInstance) => {
                // 获取角色列表
                const data = await getAccountRoles()
                // 设置角色复选框选项
                return data.map(item => ({
                    label: item.name,
                    value: item.id
                }))
            }
        }
    ]
})
